package com.zhongmeng.newsdemo.ui.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.zhongmeng.newsdemo.R;
import com.zhongmeng.newsdemo.base.BaseActivity;
import com.zhongmeng.newsdemo.ui.fragment.FocusFragment;
import com.zhongmeng.newsdemo.ui.fragment.HomeFragment;
import com.zhongmeng.newsdemo.ui.fragment.PhotoFragment;
import com.zhongmeng.newsdemo.ui.fragment.VideoFragment;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener{

    @BindView(R.id.frame_content)
    FrameLayout frameContent;
    @BindView(R.id.rb_home)
    RadioButton rbHome;
    @BindView(R.id.rb_photo)
    RadioButton rbPhoto;
    @BindView(R.id.rb_video)
    RadioButton rbVideo;
    @BindView(R.id.rb_focus)
    RadioButton rbFocus;
    @BindView(R.id.radiogroup)
    RadioGroup radiogroup;

    private Fragment[] mFragments;
    private int mIndex;
//    private FragmentManager fragmentManager;
//    private FragmentTransaction ft;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        initFragmemnt();
    }

    private void initFragmemnt() {
        HomeFragment homeFragment=new HomeFragment();
        PhotoFragment photoFragment=new PhotoFragment();
        VideoFragment videoFragment=new VideoFragment();
        FocusFragment focusFragment=new FocusFragment();

        mFragments=new Fragment[]{homeFragment,photoFragment,videoFragment,focusFragment};
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction ft=fragmentManager.beginTransaction();
        ft.add(R.id.frame_content,homeFragment).commit();

        setIndexSelect(0);
    }

    private void setIndexSelect(int index) {
        if (mIndex==index){
            return;
        }
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction ft=fragmentManager.beginTransaction();
        ft.hide(mFragments[mIndex]);
        if (!mFragments[index].isAdded()){
            ft.add(R.id.frame_content,mFragments[index]).show(mFragments[index]);
        }else
            ft.show(mFragments[index]);

        ft.commit();

        mIndex=index;
    }

    @Override
    public void addListener() {
        radiogroup.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int checkId) {
        switch (checkId){
            case R.id.rb_home:
                setIndexSelect(0);
                break;
            case R.id.rb_photo:
                setIndexSelect(1);
                break;
            case R.id.rb_video:
                setIndexSelect(2);
                break;
            default:
                setIndexSelect(3);

        }
    }


}
